package com.regentsoft.cloudback.operateAdmin.service.impl;

import com.regentsoft.cloudback.operateAdmin.dao.ActivationSubCodeMapper;
import com.regentsoft.cloudback.operateAdmin.dao.PdaMachineCodeMapper;
import com.regentsoft.cloudback.operateAdmin.entity.ActivationSubCode;
import com.regentsoft.cloudback.operateAdmin.entity.ActivationSubCodeParam;
import com.regentsoft.cloudback.operateAdmin.service.ActivationSubCodeService;
import com.regentsoft.cloudback.vo.req.ActivationSubCodeReq;
import com.regentsoft.cloudback.vo.req.CompanyActivationQueryReq;
import com.regentsoft.cloudback.vo.resp.CompanyActivationQueryResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by work on 2017/11/30.
 *
 * @author lumr
 */
@Service
public class ActivationSubCodeServiceImpl implements ActivationSubCodeService {
    @Autowired
    private ActivationSubCodeMapper mapper;
    @Autowired
    private PdaMachineCodeMapper machineCodeMapper;

    /**
     * 生成子激活码并保存到数据库
     *
     * @param companyId
     * @return
     */
    @Override
    public boolean generatorSubCode(String companyId, int from, int to,String activateCode, String creator) {
        Date date = new Date();
        Integer[] integers = new Integer[to-from];
        int j=0;
        for (int i = from; i < to; i++) {
            integers[j] = i;
            j++;
        }
        int result = mapper.generatorSubCode(new ActivationSubCodeParam(companyId, integers, activateCode, date, creator));

        return result >= 0;
    }


    @Override
    public CompanyActivationQueryResp getSubCode(CompanyActivationQueryReq queryReq) {
        CompanyActivationQueryResp resp = new CompanyActivationQueryResp(mapper.getSubCode(queryReq));
        resp.setTotal(mapper.getTotalCount(queryReq.getCode(),queryReq.getStatus()));
        return resp;
    }

    @Override
    @Transactional
    public boolean changSubCodeStatus(ActivationSubCodeReq req) {
        if (req.getStatus() == 2){
            req.setStatus(0);
            mapper.changSubCodeStatus(req);
            machineCodeMapper.deleteMachineCode(req.getCompanyId(),req.getCode(),req.getNumber());
            return true;
        }

        return mapper.changSubCodeStatus(req) > 0;
    }

	@Override
	public int getTotalCount(String code) {
		return mapper.getTotalCount(code,null);
	}

}
